package com.example.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.entity.Promotion;
import com.example.entity.PromotionParty;
import com.example.entity.PromotionPartyAdded;
import com.example.entity.PromotionRecord;
import com.example.entity.authority.Party;
import com.example.mapp.PromotionMapp;

/**
 * Created by douyx on 2018/03/18.
 */
public class PromotionUtil {

	/**
	 * 计算时间差
	 * 
	 * @param date1
	 * @param date2
	 * @param type
	 *            1:天 2:分
	 * @return
	 */
	public static Integer calcDate(String date1, String date2, String type) throws Exception {
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
		Long c = sf.parse(date1).getTime() - sf.parse(date2).getTime();
		if ("1".equals(type)) {
			return Integer.valueOf(String.valueOf((c / 1000 / 60 / 60 / 24)));
		} else {
			return Integer.valueOf(String.valueOf((c / 1000 / 60)));
		}
	}

	/**
	 * 获取上个月
	 * 
	 * @return
	 */
	public static String getLastMonth() throws Exception {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
		return format.format(c.getTime());
	}

	/**
	 * 获取当前时间
	 * 
	 * @return
	 */
	public static String getTime() throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(new Date());
	}

	public static void addEmpiric(Long userId, String type, Integer empiricValue, String content, PromotionMapp promotionMapp, boolean cert) throws Exception{
		/**
		 * 积分有改动，原先代码注释掉
		 */

		/*Map<String, Object> map = new HashMap<>();
		map.put("type", type);
		map.put("userId", userId);
		Promotion promotion = promotionMapp.findById(map);
		if (null != promotion && promotion.getId() != null) {
			promotion = PromotionUtil.calc(promotion, type, empiricValue, false);
			if (cert) {
				promotion.setUserCert("1");
			} else {
				promotion.setUserCert("0");
			}
			promotionMapp.update(promotion);
		} else {
			promotion = new Promotion();
			promotion.setType(type);
			promotion.setUserId(userId.toString());
			promotion = PromotionUtil.calc(promotion, type, empiricValue, true);
			if (cert) {
				promotion.setUserCert("1");
			} else {
				promotion.setUserCert("0");
			}
			promotionMapp.insert(promotion);
		}
		PromotionRecord promotionRecord = new PromotionRecord();
		promotionRecord.setUserId(promotion.getUserId());
		promotionRecord.setType(promotion.getType());
		promotionRecord.setEmpiricItem(content);
		promotionRecord.setAddedTime(PromotionUtil.getTime());
		promotionRecord.setEmpiricValue(empiricValue);
		promotionMapp.insertRecord(promotionRecord);*/
	}

	/**
	 * 计算最近经验值，等级，成就
	 * 
	 * @param promotion
	 * @param type
	 * @param empiricValue
	 * @param flag:
	 *            true 新增, false 更新
	 * @return
	 */
	public static Promotion calc(Promotion promotion, String type, Integer empiricValue, boolean flag) {
		if ("3".equals(type)) {
			return calcSafeManager(promotion, empiricValue, flag);
		} else if ("4".equals(type)) {
			return calcMaintOffier(promotion, empiricValue, flag);
		}

		return promotion;
	}

	/**
	 * 计算单位最新经验值
	 * 
	 * @param promotionMapp
	 * @param empiricValue
	 * @param type
	 *            1 维保单位 2 物业单位
	 * @param partyId
	 * @param itemType 1-1 上传证书 1-2 删除证书 2-1 填写资质 2-2 删除资质
	 * @return
	 */
	public static void calcParty(PromotionMapp promotionMapp, Integer empiricValue, String type, Long partyId, String content, String itemType)
			throws Exception {

		/**
		 * 积分有改动，原先代码注释掉
		 */

		/*Map<String, Object> map = new HashMap<>();
		map.put("type", type);
		map.put("userId", String.valueOf(partyId));
		Promotion promotion = promotionMapp.findById(map);
		if (null != promotion && promotion.getId() != null) {
			String month = new SimpleDateFormat("yyyyMM").format(new Date());
			Map<String, Object> map2 = new HashMap<>();
			map2.put("month", month);
			map2.put("userId", String.valueOf(partyId));
			PromotionPartyAdded promotionPartyAdded = promotionMapp.findPartyAddedById(map2);
			promotion.setEmpiricValue(empiricValue + promotion.getEmpiricValue());
			if ("1-1".equals(itemType)) {
				promotion.setUserCert("1");
			} else if ("1-2".equals(itemType)) {
				promotion.setUserCert("0");
			}
			promotionMapp.update(promotion);
			// 记录增加的积分值
			if (null != promotionPartyAdded && promotionPartyAdded.getId() != null) {
				promotionPartyAdded.setEmpiricValueAdded(empiricValue + promotionPartyAdded.getEmpiricValueAdded());
				promotionMapp.updatePartyAdded(promotionPartyAdded);
			} else {
				promotionPartyAdded = new PromotionPartyAdded();
				promotionPartyAdded.setMonth(month);
				promotionPartyAdded.setUserId(String.valueOf(partyId));
				promotionPartyAdded.setEmpiricValueAdded(empiricValue);
				promotionMapp.insertPartyAdded(promotionPartyAdded);
			}
			PromotionParty promotionParty = promotionMapp.findPartyById(partyId);
			promotionParty.setUserId(partyId + "");
			promotionParty.setType(type);
			if ("2-1".equals(itemType)) {
				promotionParty.setZzjb("1");
			} else if ("2-2".equals(itemType)) {
				promotionParty.setZzjb("0");
			}
			promotionMapp.updateParty(promotionParty);
			PromotionRecord promotionRecord1 = new PromotionRecord();
			promotionRecord1.setUserId(partyId.toString());
			promotionRecord1.setType(type);
			promotionRecord1.setEmpiricItem(content);
			promotionRecord1.setAddedTime(PromotionUtil.getTime());
			promotionRecord1.setEmpiricValue(empiricValue);
			promotionMapp.insertRecord(promotionRecord1);
		} else {
			promotion = new Promotion();
			promotion.setType(type);
			promotion.setUserId(partyId + "");
			PromotionParty promotionParty = new PromotionParty();
			promotionParty.setUserId(partyId + "");
			promotionParty.setType(type);
			Integer empiricValueAdded = 0;
			if ("2-1".equals(itemType)) {
				promotionParty.setZzjb("1");
			} else if ("2-2".equals(itemType)) {
				promotionParty.setZzjb("0");
			}
			empiricValueAdded += empiricValue;
			PromotionRecord promotionRecord = new PromotionRecord();
			promotionRecord.setUserId(partyId + "");
			promotionRecord.setType(type);
			promotionRecord.setEmpiricItem(content);
			promotionRecord.setAddedTime(PromotionUtil.getTime());
			promotionRecord.setEmpiricValue(empiricValue);
			promotionMapp.insertRecord(promotionRecord);
			promotionMapp.insertParty(promotionParty);
			promotion.setEmpiricValue(empiricValueAdded);
			promotionMapp.insert(promotion);
			// 记录增加的积分值
			PromotionPartyAdded promotionPartyAdded = new PromotionPartyAdded();
			promotionPartyAdded.setMonth(new SimpleDateFormat("yyyyMM").format(new Date()));
			promotionPartyAdded.setUserId(partyId + "");
			promotionPartyAdded.setEmpiricValueAdded(empiricValueAdded);
			promotionMapp.insertPartyAdded(promotionPartyAdded);
		}*/
		
	}

	/**
	 * 计算安全管理员最新经验值，等级，成就
	 * 
	 * @param promotion
	 * @param empiricValue
	 * @param flag
	 * @return
	 */
	private static Promotion calcSafeManager(Promotion promotion, Integer empiricValue, boolean flag) {
		if (flag) {
			promotion.setEmpiricValue(empiricValue);
			promotion.setGrade("青铜三");
		} else {
			empiricValue = promotion.getEmpiricValue() + empiricValue;
			promotion.setEmpiricValue(empiricValue);
			if (empiricValue < 50) {
				promotion.setGrade("青铜三");
			} else if (empiricValue < 500) {
				promotion.setGrade("青铜二");
			} else if (empiricValue < 2500) {
				promotion.setGrade("青铜一");
				promotion.setAchieve("初出茅庐");
			} else if (empiricValue < 5000) {
				promotion.setGrade("白银三");
				promotion.setAchieve("初出茅庐");
			} else if (empiricValue < 7500) {
				promotion.setGrade("白银二");
				promotion.setAchieve("初出茅庐");
			} else if (empiricValue < 11500) {
				promotion.setGrade("白银一");
				promotion.setAchieve("小有成就");
			} else if (empiricValue < 14000) {
				promotion.setGrade("黄金三");
				promotion.setAchieve("小有成就");
			} else if (empiricValue < 16500) {
				promotion.setGrade("黄金二");
				promotion.setAchieve("小有成就");
			} else if (empiricValue < 20000) {
				promotion.setGrade("黄金一");
				promotion.setAchieve("电梯卫士");
			} else if (empiricValue < 23000) {
				promotion.setGrade("钻石三");
				promotion.setAchieve("电梯卫士");
			} else if (empiricValue < 26000) {
				promotion.setGrade("钻石二");
				promotion.setAchieve("电梯卫士");
			} else if (empiricValue < 30000) {
				promotion.setGrade("钻石一");
				promotion.setAchieve("电梯大咖");
			} else {
				promotion.setGrade("大师");
				promotion.setAchieve("电梯大咖");
			}
		}
		return promotion;
	}

	/**
	 * 计算维保员最新经验值，等级，成就
	 * 
	 * @param promotion
	 * @param empiricValue
	 * @param flag
	 * @return
	 */
	private static Promotion calcMaintOffier(Promotion promotion, Integer empiricValue, boolean flag) {
		if (flag) {
			promotion.setEmpiricValue(empiricValue);
			promotion.setGrade("青铜三");
		} else {
			empiricValue = promotion.getEmpiricValue() + empiricValue;
			promotion.setEmpiricValue(empiricValue);
			if (empiricValue < 50) {
				promotion.setGrade("青铜三");
			} else if (empiricValue < 100) {
				promotion.setGrade("青铜二");
			} else if (empiricValue < 200) {
				promotion.setGrade("青铜一");
				promotion.setAchieve("初出茅庐");
			} else if (empiricValue < 300) {
				promotion.setGrade("白银三");
				promotion.setAchieve("初出茅庐");
			} else if (empiricValue < 500) {
				promotion.setGrade("白银二");
				promotion.setAchieve("初出茅庐");
			} else if (empiricValue < 1000) {
				promotion.setGrade("白银一");
				promotion.setAchieve("小有成就");
			} else if (empiricValue < 2000) {
				promotion.setGrade("黄金三");
				promotion.setAchieve("小有成就");
			} else if (empiricValue < 3000) {
				promotion.setGrade("黄金二");
				promotion.setAchieve("小有成就");
			} else if (empiricValue < 4500) {
				promotion.setGrade("黄金一");
				promotion.setAchieve("电梯卫士");
			} else if (empiricValue < 6000) {
				promotion.setGrade("钻石三");
				promotion.setAchieve("电梯卫士");
			} else if (empiricValue < 7500) {
				promotion.setGrade("钻石二");
				promotion.setAchieve("电梯卫士");
			} else if (empiricValue < 10000) {
				promotion.setGrade("钻石一");
				promotion.setAchieve("维保大咖");
			} else {
				promotion.setGrade("大师");
				promotion.setAchieve("维保大咖");
			}
		}
		return promotion;
	}

	/**
	 * 获取成就列表
	 * 
	 * @param achieve
	 * @return
	 */
	public static List<String> getachieveList(String achieve) {
		List<String> achieveList = new ArrayList<>();
		if (null == achieve || "".equals(achieve)) {
			return achieveList;
		}
		achieveList.add(achieve);
		if ("小有成就".equals(achieve)) {
			achieveList.add("初出茅庐");
		} else if ("电梯卫士".equals(achieve)) {
			achieveList.add("初出茅庐");
			achieveList.add("小有成就");
		} else if ("维保大咖".equals(achieve) || "电梯大咖".equals(achieve)) {
			achieveList.add("初出茅庐");
			achieveList.add("小有成就");
			achieveList.add("电梯卫士");
		}
		return achieveList;
	}
}
